package com.ds.lens.data.service.dao;

import com.github.pagehelper.Page;
import com.ds.lens.data.service.dao.entity.LensMonitorStateMain;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface LensMonitorStateMainDao {

    String COLUMNS = " id, monitor_id, version, state, target, last_time ";

    @Insert({
            "insert into lens_monitor_state_main (monitor_id, version, state, target, last_time)",
            "values (#{monitorId,jdbcType=BIGINT}, #{version,jdbcType=INTEGER}, ",
            "#{state,jdbcType=SMALLINT}, #{target,jdbcType=VARCHAR}, #{lastTime,jdbcType=TIMESTAMP})"
    })
    int insert(LensMonitorStateMain record);

    @Insert({
            "<script>",
            "insert into lens_monitor_state_main (monitor_id, version, state, target, last_time)",
            "values",
            "<foreach collection='records' item='record' separator=','>",
            "(#{record.monitorId}, #{record.version}, #{record.state}, #{record.target},#{record.lastTime})",
            "</foreach>",
            "</script>"
    })
    int batchInsert(@Param("records") List<LensMonitorStateMain> records);


    @Select({
            "select",
            COLUMNS,
            "from lens_monitor_state_main",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "monitorState", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "monitor_id", property = "monitorId", jdbcType = JdbcType.BIGINT),
            @Result(column = "version", property = "version", jdbcType = JdbcType.INTEGER),
            @Result(column = "state", property = "state", jdbcType = JdbcType.SMALLINT),
            @Result(column = "target", property = "target", jdbcType = JdbcType.VARCHAR),
            @Result(column = "last_time", property = "lastTime", jdbcType = JdbcType.TIMESTAMP)
    })
    LensMonitorStateMain selectByPrimaryKey(Long id);

    @Select({
            "select ",
            COLUMNS,
            "from lens_monitor_state_main",
            "where monitor_id = #{monitorId} and version = #{version} and target = #{target} "
    })
    @ResultMap("monitorState")
    LensMonitorStateMain selectLastStateByMonitor(@Param("monitorId") Long monitorId, @Param("version") Integer version,
                                                  @Param("target") String target);

    @Select({
            "select ",
            COLUMNS,
            "from lens_monitor_state_main",
            "where monitor_id = #{monitorId} and version = #{version} "
    })
    @ResultMap("monitorState")
    List<LensMonitorStateMain> selectByMonitor(@Param("monitorId") Long monitorId, @Param("version") Integer version);


    @Update({
            "update lens_monitor_state_main set state = #{state}, last_time=#{lastTime} ",
            "where monitor_id = #{monitorId} and version = #{version} and target = #{target}"
    })
    int updateByMonitor(@Param("monitorId") Long monitorId, @Param("version") Integer version,
                        @Param("state") Integer state, @Param("lastTime") Date lastTime,
                        @Param("target") String target);


    @Update({
            "update lens_monitor_state_main set state = #{state}, last_time=#{lastTime} ",
            "where id = #{id}"
    })
    int updateById(@Param("id") Long id, @Param("state") Integer state, @Param("lastTime") Date lastTime);

    @Update({
            "update lens_monitor_state_main set state = #{state}, last_time=#{lastTime} ",
            "where monitor_id = #{monitorId} and version = #{version} "
    })
    int updateAllByMonitor(@Param("monitorId") Long monitorId, @Param("version") Integer version,
                           @Param("state") Integer state, @Param("lastTime") Date lastTime);

    @Select({
            "<script>",
            "select ",
            COLUMNS,
            "from lens_monitor_state_main ",
            "where monitor_id = #{monitorId,jdbcType=BIGINT} and version = #{version,jdbcType=INTEGER} ",
            "<if test=\"target!=null and target != '' \"> and target = #{target,jdbcType=VARCHAR} </if>",
            "<if test='states != null and states.size > 0'> and state in ",
            "<foreach collection='states' item='state' separator=',' open='(' close=')' >",
            "#{state}",
            "</foreach>",
            "</if>",
            "order by last_time desc",
            "</script>"
    })
    @ResultMap("monitorState")
    Page<LensMonitorStateMain> selectTargetsByPage(@Param("monitorId") Long monitorId,
                                                   @Param("version") Integer version,
                                                   @Param("target") String target,
                                                   @Param("states") List<Integer> states);

}
